package com.scau.bookstore.api.controller;

import com.scau.bookstore.api.annotation.LoginAuthToken;
import com.scau.bookstore.api.annotation.PassToken;
import com.scau.bookstore.api.util.JwtUtil;
import com.scau.bookstore.common.entity.*;
import com.scau.bookstore.common.pojo.Address;
import com.scau.bookstore.common.pojo.User;
import com.scau.bookstore.contract.service.AddressService;
import com.scau.bookstore.contract.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AddressService addressService;

    /**
     * 登录
     *
     * @param searchMap
     * @return
     */
    @PassToken
    @PostMapping("/login")
    public Result login(@RequestBody Map searchMap) {
        List<User> userList = userService.findSearch(searchMap);
        if (userList != null && userList.size() > 0) {
            User user = userList.get(0);
            String token = JwtUtil.createToken(user.getId(), user.getUsername());
            return new Result(true, StatusCode.OK, "登录成功", token);
        } else {
            return new Result(true, StatusCode.ERROR, "用户名或密码错误");
        }

    }

    /**
     * 登出
     */
    @PassToken
    @GetMapping("/logout")
    public Result logout(String token) {
        System.out.println(token);
        return new Result(true, StatusCode.OK, "注销成功");
    }

    /**
     * 注册
     */
    @PassToken
    @PostMapping("/register")
    public Result register(@RequestBody RegisterForm registerForm) {
        User user = User.builder().username(registerForm.getUsername()).password(registerForm.getPassword()).roleId(1L).build();
        User user1 = userService.add(user);
        Address address = Address.builder().address(registerForm.getAddress()).phone(registerForm.getPhone()).receiveName(registerForm.getReceiveName()).userId(user1.getId()).build();
        addressService.add(address);
        return new Result(true, StatusCode.OK, "注册成功");
    }

    /**
     * 个人信息
     *
     * @return
     */
    @LoginAuthToken
    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        Long userId = JwtUtil.getIdByToken(token);
        User user = userService.findById(userId);
        return new Result(true, StatusCode.OK, "获取个人信息成功", user);
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", userService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable Long id) {
        return new Result(true, StatusCode.OK, "查询成功", userService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<User> pageList = userService.findSearch(searchMap, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<User>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", userService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param user
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody User user) {
        userService.add(user);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param user
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody User user, @PathVariable Long id) {
        user.setId(id);
        userService.update(user);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Long id) {
        userService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

}
